var isHome = true;
var isChart = true;
var myChart;
var chartOption;
(function () {
    loadUrl();
    /**初始化站点**/
    initOrigin();
    /**检测是否选择站点**/
    checkStation();
    // setTimeout(function () {
    //     isLook();
    // }, 500);

    $(".footer-i").on("click", function () {
        var url = $(this).find("a").attr("data-url");
        var isShow = $(this).find("a").attr("data-show");
        if (isNull(url)) {
            wapAlert("功能暂未开放");
            return;
        }

        isChart = false;
        if (!isNull(isShow)) {
            if (isShow == 1)
                isHome = true;
            else
                isShow = false;

            $(this).addClass('footer-i-active');
            $(this).siblings().removeClass('footer-i-active');
            $(this).find('.footer-i-img1').removeClass('dn');
            $(this).find('.footer-i-img2').addClass('dn');
            $(this).siblings().find('.footer-i-img1').addClass('dn');
            $(this).siblings().find('.footer-i-img2').removeClass('dn');
            $(".surker-main").empty().load(url);
        } else {
            window.location.href = url;
        }
    });

    /**站点切换**/
    $(".user-station-btn").on("click", function () {
        wapAlert("功能暂未开放")
    });

    /**用户消息中心**/
    $(".user-message-btn").on("click", function () {
        var url = $(this).attr("data-url");
        if (isNull(url))
            wapAlert("功能暂未开放");
        else

            window.location.href = url;
    });

    remComputer()
    window.onresize = function () {

        remComputer()
    }

    function remComputer() {
        var w = document.documentElement.clientWidth || document.body.clientWidth
        console.log(w)
        var doc = document.getElementsByTagName("html")[0]
        // console.log(doc)
        if (w > 750) {
            doc.style.fontSize = 100 + 'px'
        } else {
            doc.style.fontSize = w / 7.5 + 'px'
        }
    }

    function loadUrl() {
        var url = $("#url").val();
        if (!isNull(url)) {
            var footer = $('.footer-i');
            $.each(footer, function (index, obj) {
               var data = $(obj).find("a").attr("data-url")
               if (!isNull(data) && data == url) {
                    $(obj).addClass('footer-i-active');
                    $(obj).siblings().removeClass('footer-i-active');
                    $(obj).find('.footer-i-img1').removeClass('dn');
                    $(obj).find('.footer-i-img2').addClass('dn');
                    $(obj).siblings().find('.footer-i-img1').addClass('dn');
                    $(obj).siblings().find('.footer-i-img2').removeClass('dn');
                }
            });
            $(".surker-main").empty().load(url);
        } else {
            $(".surker-main").empty().load("/cloud/home");
        }

    }

    /**点击站点选项**/
    $('.station-list').on('click', 'dt', function () {
        $(this).siblings().find(".icon-confirm").removeClass("confirm-yes");
        $(this).find(".icon-confirm").addClass("confirm-yes");
        orgId = $(this).find(".icon-confirm").attr('data-id');
    });

    $('.cancel-station').on('click', function () {
        $('.TipsWindown').hide();
    });

    /**点击站点**/
    $('.toolbar-station-btn').on('click', function () {
        $('.TipsWindown').show();
    });

    /**选择站点**/
    $('.confirm-station').on('click', function () {
        if (isNull(orgId) || orgId == -1) {
            wapAlert("请选择站点！");
            return;
        }
        wapLoading.show();
        $.ajax({
            type: "POST",
            url: "/console/chose/station",
            async: true,
            dataType: 'json',
            timeout: 15000,
            data: {originId: orgId},
            success: function (res) {
                wapLoading.hide();
                if (res.code == 0) {
                    $('.toolbar-station').attr('data-id', res.data.originId);
                    $('.toolbar-station').html('站点(' + res.data.originName + ')');
                    $('.TipsWindown').hide();
                    if (isHome) {
                        initBoard();
                        setTimeout(function () {
                            /**初始看板数据**/
                            loadReport();
                        }, 500);
                    }
                } else {
                    wapAlert(res.msg);
                    return;
                }

            },
            error: function (res) {
                // wapAlert('系统异常,请稍后再试');
                wapLoading.hide();
                return;
            },
            complete: function (XMLHttpRequest, status) {
                if (status == 'timeout') {
                    wapAlert('网络链接超时,请稍后再试');
                    wapLoading.hide();
                    return;
                }
            }
        });
    });


    /**检查站点**/
    function checkStation() {
        var originId = $('.toolbar-station').attr('data-id');
        if (!isNull(originId))
            return;

        // wapLoading.show();
        $.ajax({
            type: "POST",
            url: "/console/check/station",
            async: true,
            dataType: 'json',
            timeout: 15000,
            success: function (res) {
                // wapLoading.hide();
                if (res.code == 0) {
                    $('.toolbar-station').attr('data-id', res.data.originId);
                    $('.toolbar-station').html('站点(' + res.data.originName + ')');
                    if (isHome) {
                        setTimeout(function () {
                            /**初始看板数据**/
                            loadReport();
                        }, 500);
                    }
                } else {
                    $('.TipsWindown').show();
                    return;
                }

            },
            error: function (res) {
                // wapAlert(res.msg);
                // wapLoading.hide();
                return;
            },
            complete: function (XMLHttpRequest, status) {
                if (status == 'timeout') {
                    wapAlert('网络链接超时,请稍后再试');
                    // wapLoading.hide();
                    return;
                }
            }
        });
    }


    function isLook() {
        $.ajax({
            type: "post",
            url: '/message/query/islook',
            async: true,
            dataType: 'json',
            timeout: 15000,
            data: {type: 2, status: 0},
            success: function (res) {
                // console.log(res);
                if (res.code == 0) {
                    var count = res.data;
                    if (count > 0) {
                        $(".weui-badge").show();
                        $(".weui-badge").text(count);
                    }
                }
            },
            error: function (res) {
                // wapAlert(res.msg);
                return;
            },
            complete: function (XMLHttpRequest, status) {
                if (status == 'timeout') {
                    wapAlert('网络链接超时,请稍后再试');
                    return;
                }
            }
        });
    }

    /**初始化站点**/
    function initOrigin() {
        $.ajax({
            type: "post",
            url: '/sys_origin/query/user/origin',
            async: true,
            dataType: 'json',
            timeout: 15000,
            data: {type: 2, status: 0},
            success: function (res) {
                if (res.code == 0) {
                    var html = '';
                    if (res.data != null && res.data.length > 0) {
                        $.each(res.data, function (index, val) {
                            if (index == 0) {
                                html += '<dt><span>' + val.originName + '</span> <i data-id="' + val.id +
                                    '" class="wnsM icon-confirm confirm-yes"></i></dt>';
                                orgId = val.id;
                            } else
                                html += '<dt><span>' + val.originName + '</span> <i data-id="' + val.id +
                                    '" class="wnsM icon-confirm"></i></dt>';
                        });
                        $('.station-list').empty().append(html);
                    } else {
                        $('.station-list').empty().append("<dt><span>您未关联站点,请联系管理人员</span></dt>");
                    }
                } else {
                    $('.station-list').empty().append("<dt><span>您未关联站点,请联系管理人员</span></dt>");
                }
            },
            error: function (res) {
                // wapAlert(res.msg);
                return;
            },
            complete: function (XMLHttpRequest, status) {
                if (status == 'timeout') {
                    wapAlert('网络链接超时,请稍后再试');
                    return;
                }
            }
        });
    }
})()
